﻿using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Web;
using MvcCms.Service.Logging;

namespace MvcCms.Service.Code
{
    public static class DevCheck
    {
        public static bool IsDev()
        {
            var ip = string.Empty;
            var urlHost = HttpContext.Current.Request.Url.Host.ToLower();

            if (urlHost.Contains("test")) { return true; }

            //also check for this in case iis express is being used with the computer name or ip address
            var computerHost = System.Net.Dns.GetHostName().ToLower();
            var ipv4Addresses = Array.FindAll(Dns.GetHostEntry(string.Empty).AddressList, a => a.AddressFamily == AddressFamily.InterNetwork);
            if (ipv4Addresses.Length > 0) { ip = ipv4Addresses.First().ToString(); }

            //if this is a localhost request we want the port if it is not usually we don't, if you do want a non localhost port do it here.
            if (urlHost != "localhost" && urlHost != computerHost && urlHost != ip)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// Takes the name of the desired production server as a paramater and if the computer this is running on isn't a match then it is considered dev.  Mostly used for scheduler apps that run on their own thread.
        /// </summary>
        /// <returns></returns>
        public static bool IsDev(string productionServerName)
        {
            var computerHost = System.Net.Dns.GetHostName().ToLower();
            if (computerHost.ToLower() == productionServerName.ToLower())
            {
                return false;
            }
            return true;
        }
    }
}
